//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <string.h>
#include <elatypes.h>

EXTERN_C wchar_t * CDECL wcsstr(const wchar_t * string, const wchar_t * strCharSet)
{
/*
    register wchar_t * __res;
#if !defined(_arm)
    int d0, d1;
    asm __volatile__(
        "movl %6,%%edi\n\t"
        "repne\n\t"
        "scasw\n\t"
        "notl %%ecx\n\t"
        "decl %%ecx\n\t"
        "movl %%ecx,%%edx\n"
        "1:\tmovl %6,%%edi\n\t"
        "movl %%esi,%%eax\n\t"
        "movl %%edx,%%ecx\n\t"
        "repe\n\t"
        "cmpsw\n\t"
        "je 2f\n\t"
        "xchgl %%eax,%%esi\n\t"
        "addl $2, %%esi\n\t"
        "cmpw $0,-2(%%eax)\n\t"
        "jne 1b\n\t"
        "xorl %%eax,%%eax\n\t"
        "2:"
        :"=a" (__res), "=&c" (d0), "=&S" (d1)
        :"0" (0), "1" (0xffffffff), "2" (string), "g" (strCharSet)
        :"dx", "di");
#else
    if (string != NULL && strCharSet != NULL && *strCharSet != '\0') {
        wchar_t sub = *strCharSet++;
        size_t len = wcslen(strCharSet);
        do {
            wchar_t ch;
            do {
                ch = *string++;
                if (ch == 0)
                    return NULL;
            } while (ch != sub);

            if (len == 0)
                return ((wchar_t *)(string-1));
        } while (wcsncmp(string, strCharSet, len));
        string--;
    }
    return ((wchar_t *)string);
#endif

    return __res;
*/
    if (*strCharSet != '\0') {
        wchar_t sub = *strCharSet++;
        size_t len = wcslen(strCharSet);
        do {
            wchar_t ch;
            do {
                ch = *string++;
                if (ch == 0)
                    return NULL;
            } while (ch != sub);

            if (len == 0)
                return ((wchar_t *)(string-1));
        } while (wcsncmp(string, strCharSet, len));
        string--;
    }
    return ((wchar_t *)string);
}

EXTERN_C char * CDECL strstr(const char *s, const char *find)
{
/*
    register char * __res;
#if !defined(_arm)
    int d0, d1;
    asm __volatile__(
        "movl %6,%%edi\n\t"
        "repne\n\t"
        "scasb\n\t"
        "notl %%ecx\n\t"
        "decl %%ecx\n\t"
        "movl %%ecx,%%edx\n"
        "1:\tmovl %6,%%edi\n\t"
        "movl %%esi,%%eax\n\t"
        "movl %%edx,%%ecx\n\t"
        "repe\n\t"
        "cmpsb\n\t"
        "je 2f\n\t"
        "xchgl %%eax,%%esi\n\t"
        "incl %%esi\n\t"
        "cmpb $0,-1(%%eax)\n\t"
        "jne 1b\n\t"
        "xorl %%eax,%%eax\n\t"
        "2:"
        :"=a" (__res), "=&c" (d0), "=&S" (d1)
        :"0" (0), "1" (0xffffffff), "2" (s), "g" (find)
        :"dx", "di");
#else
    char c, sc;
    size_t len;

    if ((c = *find++) != 0) {
        len = strlen(find);
        do {
            do {
                if ((sc = *s++) == 0)
                    return ((char *) 0);
            } while (sc != c);
        } while (strncmp(s, find, len) != 0);
        s--;
    }
    return ((char *) s);
#endif

    return __res;
*/
    char c, sc;
    size_t len;

    if ((c = *find++) != 0) {
        len = strlen(find);
        do {
            do {
                if ((sc = *s++) == 0)
                    return ((char *) 0);
            } while (sc != c);
        } while (strncmp(s, find, len) != 0);
        s--;
    }
    return ((char *) s);
}
